package business_opportunity

import (
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/http_server/controller"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/common/errcode"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/dto/business_opportunity"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/utils"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/service/business_opportunity/project"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/gin-gonic/gin"
)

type BuildingController struct {
	controller.BaseController
	buildingService project.BuildingServiceIface
}

func NewBuildingController(
	buildingService project.BuildingServiceIface,
) *BuildingController {
	return &BuildingController{buildingService: buildingService}
}

// RegisterRouter
// 注册路由
func (c *BuildingController) RegisterRouter(g *gin.RouterGroup) {
	// 在建项目列表
	g.POST("/project/building/list", c.BuildingList)
	// 在建项目-详情
	g.GET("/project/building/detail", c.GetBuildingDetailBasic)
	// 在建项目-详情页指标数量
	g.GET("/project/building/detail-target-num", c.GetBuildingDetailTargetNum)
	// 在建项目-详情-获取招标列表
	g.GET("/project/building/detail-bid-list", c.GetBuildingDetailBidList)
	// 在建项目详情-获取合作单位
	g.GET("/project/building/detail-participating-units", c.GetBuildingDetailParticipatingUnits)
	// 在建项目-详情-获取合同登记列表
	g.GET("/project/building/detail-contract-list", c.GetBuildingDetailContractList)
	// 在建项目-详情-获取施工图审列表
	g.GET("/project/building/detail-working-drawing-audit", c.GetBuildingDetailWorkingDrawingAudit)
	// 在建项目-详情-施工许可列表
	g.GET("/project/building/detail-construction-permit", c.GetBuildingDetailConstructionPermit)
	// 在建项目-详情-竣工备案列表
	g.GET("/project/building/detail-completion-filing", c.GetBuildingDetailCompletionFiling)
}

func (c *BuildingController) BuildingList(ctx *gin.Context) {
	// 1. 校验参数
	request := &business_opportunity.BuildingListRequest{}
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}
	// 2. 处理逻辑
	response, err := c.buildingService.BuildingList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "BuildingList err, request:%v, err:%s ", request, err.Error())
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	// 返回结果
	c.Success(ctx, response)
}

func (c *BuildingController) GetBuildingDetailBasic(ctx *gin.Context) {
	// 1. 校验参数
	request := business_opportunity.GetBuildingDetailRequest{}
	if err := ctx.ShouldBindQuery(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}
	// 2. 处理逻辑
	response, err := c.buildingService.GetBuildingDetailBasic(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailBasic err, request:%v, err:%s ", request, err.Error())
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	// 返回结果
	c.Success(ctx, response)
}

func (c *BuildingController) GetBuildingDetailTargetNum(ctx *gin.Context) {
	// 1. 校验参数
	request := business_opportunity.GetBuildingDetailRequest{}
	if err := ctx.ShouldBindQuery(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}
	// 2. 处理逻辑
	response, err := c.buildingService.GetBuildingDetailTargetNum(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailTargetNum err, request:%v, err:%s ", request, err.Error())
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	// 返回结果
	c.Success(ctx, response)
}

func (c *BuildingController) GetBuildingDetailBidList(ctx *gin.Context) {
	// 1. 校验参数
	request := business_opportunity.GetBuildingDetailRequest{}
	if request.Page == 0 {
		request.Page = 1
	}
	if request.PageSize == 0 {
		request.PageSize = 10
	}
	if err := ctx.ShouldBindQuery(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}
	// 2. 处理逻辑
	response, err := c.buildingService.GetBuildingDetailBidList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailBidList err, request:%v, err:%s ", request, err.Error())
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	// 返回结果
	c.Success(ctx, response)
}

func (c *BuildingController) GetBuildingDetailParticipatingUnits(ctx *gin.Context) {
	// 1. 校验参数
	request := business_opportunity.GetBuildingDetailRequest{}
	if request.Page == 0 {
		request.Page = 1
	}
	if request.PageSize == 0 {
		request.PageSize = 10
	}
	if err := ctx.ShouldBindQuery(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}
	// 2. 处理逻辑
	response, err := c.buildingService.GetBuildingDetailParticipatingUnits(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailParticipatingUnits err, request:%v, err:%s ", request, err.Error())
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	// 返回结果
	c.Success(ctx, response)
}

func (c *BuildingController) GetBuildingDetailContractList(ctx *gin.Context) {
	// 1. 校验参数
	request := business_opportunity.GetBuildingDetailRequest{}
	if request.Page == 0 {
		request.Page = 1
	}
	if request.PageSize == 0 {
		request.PageSize = 10
	}
	if err := ctx.ShouldBindQuery(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}
	// 2. 处理逻辑
	response, err := c.buildingService.GetBuildingDetailContractList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailContractList err, request:%v, err:%s ", request, err.Error())
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	// 返回结果
	c.Success(ctx, response)
}

func (c *BuildingController) GetBuildingDetailWorkingDrawingAudit(ctx *gin.Context) {
	// 1. 校验参数
	request := business_opportunity.GetBuildingDetailRequest{}
	if request.Page == 0 {
		request.Page = 1
	}
	if request.PageSize == 0 {
		request.PageSize = 10
	}
	if err := ctx.ShouldBindQuery(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}
	// 2. 处理逻辑
	response, err := c.buildingService.GetBuildingDetailWorkingDrawingAudit(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailWorkingDrawingAudit err, request:%v, err:%s ", request, err.Error())
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	// 返回结果
	c.Success(ctx, response)
}

func (c *BuildingController) GetBuildingDetailConstructionPermit(ctx *gin.Context) {
	// 1. 校验参数
	request := business_opportunity.GetBuildingDetailRequest{}
	if request.Page == 0 {
		request.Page = 1
	}
	if request.PageSize == 0 {
		request.PageSize = 10
	}
	if err := ctx.ShouldBindQuery(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}
	// 2. 处理逻辑
	response, err := c.buildingService.GetBuildingDetailConstructionPermit(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailConstructionPermit err, request:%v, err:%s ", request, err.Error())
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	// 返回结果
	c.Success(ctx, response)
}

func (c *BuildingController) GetBuildingDetailCompletionFiling(ctx *gin.Context) {
	// 1. 校验参数
	request := business_opportunity.GetBuildingDetailRequest{}
	if request.Page == 0 {
		request.Page = 1
	}
	if request.PageSize == 0 {
		request.PageSize = 10
	}
	if err := ctx.ShouldBindQuery(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}
	// 2. 处理逻辑
	response, err := c.buildingService.GetBuildingDetailCompletionFiling(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailCompletionFiling err, request:%v, err:%s ", request, err.Error())
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	// 返回结果
	c.Success(ctx, response)
}
